package RevolCraft.Blocks;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;

import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.asm.SideOnly;
import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player;
import cpw.mods.fml.common.FMLCommonHandler;
import net.minecraft.src.Block;
import net.minecraft.src.BlockContainer;
import net.minecraft.src.EntityLiving;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.IBlockAccess;
import net.minecraft.src.Material;
import net.minecraft.src.MathHelper;
import net.minecraft.src.Packet250CustomPayload;
import net.minecraft.src.TileEntity;
import net.minecraft.src.World;
import net.minecraft.src.WorldGenHellLava;
import RevolCraft.RevolCraft;
import RevolCraft.TileEntities.TileEntityGenerator;

public abstract class BlockMachine extends BlockContainer {
	
	public BlockMachine(int id, int icon, Material material)
	{
		super(id, icon, material);
	}
	
	public void onBlockPlacedBy(World world, int x, int y, int z, EntityLiving entityLiving)
    {
        int direction = MathHelper.floor_double((double)(entityLiving.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;

        world.setBlockMetadataWithNotify(x, y, z, direction);
        //System.out.println(direction);
    }
	
	public boolean onBlockActivated(World world, int x, int y, int z,
            EntityPlayer player, int idk, float what, float these, float are)
	{
		TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
        if (tileEntity == null || player.isSneaking()) {
                return false;
        }
        
        buildAndSendPacket(tileEntity, (Player)player);
        
        //opens gui, to be implemented later
        player.openGui(RevolCraft.instance, 0, world, x, y, z);
        
        return true;
	}
	
	public abstract void buildAndSendPacket(TileEntity tileEntity, Player player);
	
	public void onBlockAdded(World world, int x, int y, int z)
	{
		super.onBlockAdded(world, x, y, z);
		world.setBlockTileEntity(x, y, z, createTileEntity(world, 0));
	}
	
	public void breakBlock(World world, int x, int y, int z, int par5, int par6)
	{
		super.breakBlock(world, x, y, z, par5, par6);
		world.removeBlockTileEntity(x, y, z);
	}
	
	public boolean hasTileEntity(int metadata)
	{
		return true;
	}
	
	@Override
	public abstract TileEntity createTileEntity(World world, int metadata);
	
	@Override
	public boolean isOpaqueCube()
	{
		return false;
	}
	
	@Override
	public boolean renderAsNormalBlock()
	{
		return false;
	}
	
	@Override
	public int getRenderType()
    {
        return -1;
    }
}
